//
//  LoginViewController.m
//  project
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "LoginViewController.h"
#import "PKRequestManager.h"

@interface LoginViewController ()

@property (nonatomic, strong)UITextField *userTextField;
@property (nonatomic, strong)UITextField *passwordTextField;
@property (nonatomic, strong)UIButton *loginButton;

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.backgroundColor = [UIColor magentaColor];
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(kWidth / 2 - 20, 30, 40, 20);
    label.text = @"登录";
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    self.navigationItem.title = @"登录";

    self.view.backgroundColor = [[UIColor alloc]initWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
   
    
    [self addView];
    
    
}

-(void)addView
{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 120, kWidth, 100)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 0.5)];
    lineView.backgroundColor = [[UIColor alloc]initWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];;
    [view addSubview:lineView];
    
    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(20, 50, kWidth - 20, 0.5)];
    lineView2.backgroundColor = [[UIColor alloc]initWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
    [view addSubview:lineView2];
    
    UIView *lineVie3 = [[UIView alloc]initWithFrame:CGRectMake(0, 99, kWidth, 0.5)];
    lineVie3.backgroundColor = [[UIColor alloc]initWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];;
    [view addSubview:lineVie3];
    
    _userTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 130, kWidth - 40, 30)];
    _userTextField.placeholder = @"手机号";
    _userTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:_userTextField];
    
    _passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 180, kWidth - 40, 30)];
    _passwordTextField.placeholder = @"密码";
    _passwordTextField.secureTextEntry = YES;

    [self.view addSubview:_passwordTextField];
    
    
    _loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _loginButton.layer.cornerRadius = 5;
    _loginButton.backgroundColor = [[UIColor alloc]initWithRed:221/255.0 green:160/255.0 blue:221/255.0 alpha:1];
    _loginButton.frame = CGRectMake(20, 250, kWidth - 40, 30);
    [_loginButton addTarget:self action:@selector(loginButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginButton];
    
}

-(void)loginButton:(UIButton *)button
{
    NSString *userName = _userTextField.text;
    NSString *password = _passwordTextField.text;
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"mobile"] = userName;
    parDic[@"password"] = password;

    [PKRequestManager requestWithTypeP:POST urlString:@"http://api.liwushuo.com/v2/account/signin" parDic:parDic finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dic);
        if ([dic[@"message"] isEqualToString:@"OK"])
        {

            NSLog(@"登录成功");
            NSString *nickname = dic[@"data"][@"nickname"];
            NSString *nameid = dic[@"data"][@"id"];
            NSString *avatar_url = dic[@"data"][@"avatar_url"];
            // 登录成功后保存信息到本地
            [[NSUserDefaults standardUserDefaults] setValue:nickname forKey:@"nickname"];
            [[NSUserDefaults standardUserDefaults] setValue:nameid forKey:@"nameid"];
            [[NSUserDefaults standardUserDefaults] setValue:avatar_url forKey:@"avatar_url"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Login" object:nil];
            [PKRequestManager requestWithTypeP:GET urlString:@"http://api.liwushuo.com/v2/holidays/my_holiday?updated_at=-1" parDic:nil finish:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                [[NSUserDefaults standardUserDefaults] setValue: [NSString stringWithFormat:@"%@",dic[@"data"][@"updated_at"] ]forKey:@"LoginId"];
                NSLog(@"%@",dic[@"data"][@"updated_at"]);
                
            } error:^(NSError *error) {
                
            }];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else
        {
            UIAlertView *alertVie = [[UIAlertView alloc]initWithTitle:@"登录失败" message:@"用户名或密码不正确" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertVie show];
        }
        
    } error:^(NSError *error) {
        NSLog(@"%@", error);
    }];

    
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)back:(UIBarButtonItem *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
